package demo;

import com.myt.matp.*;
import com.myt.matp.core.*;
import com.myt.matp.gui.*;
import com.myt.matp.utils.Platform;

public final class ModWidgets extends Module  {

    // Controls
    private static final int CTRL_PANEL = 1;
    private static final int CTRL_MENU_MAIN = 2;
    private static final int CTRL_MENU_ADDC = 3;
    private static final int CTRL_BUTT_MENU = 4;
    private static final int CTRL_BUTT_ENTE = 5;
    private static final int CTRL_BUTT_BACK = 6;

    // "Main", menu options
    private static final int MENU_MAIN_EXIT = 100;
    private static final int MENU_MAIN_DELE = 101;
    private static final int MENU_MAIN_MOUP = 102;
    private static final int MENU_MAIN_MODW = 103;
    private static final int MENU_MAIN_ADDC = 104;

    // "Add ctrl", menu options
    private static final int MENU_ADDC_EDIT = 200; // Control edit
    private static final int MENU_ADDC_TEXT = 201; // Control text
    private static final int MENU_ADDC_TITL = 202; // Control title
    private static final int MENU_ADDC_LABE = 203; // Control label
    private static final int MENU_ADDC_NUME = 204; // Control numeric
    private static final int MENU_ADDC_DECI = 205; // Control decimal
    private static final int MENU_ADDC_PERC = 206; // Control percent
    private static final int MENU_ADDC_IPAD = 207; // Control IP address
    private static final int MENU_ADDC_REAL = 208; // Control real
    private static final int MENU_ADDC_BANE = 209; // Control banner
    private static final int MENU_ADDC_LIST = 210; // Control list
    private static final int MENU_ADDC_COMB = 211; // Control combo box
    private static final int MENU_ADDC_CHKB = 212; // Control check box
    private static final int MENU_ADDC_RADB = 213; // Control radio button
    private static final int MENU_ADDC_TIME = 214; // Control time
    private static final int MENU_ADDC_DATE = 215; // Control date
    private static final int MENU_ADDC_BACK = 216; // Control date

    // Menus
    private UIMenu m_MenuMain = null;
    private UIMenu m_MenuAddC = null;


    public ModWidgets(int id, MATlet m) {
	super(id, m);
    }

    public void work(final Msg msg) {
	switch (msg.getId()) {
	case Executable.STATE_INIT:
	    workInit(msg);
	    break;
	default:
	    break;
	}
    }

    private void workInit(final Msg msg) {
	GUI gui;
	UIPanel panel;
	UISoft soft;
	UIMenu menu;
	UIButton button;
	UIEdit edit;

	gui = getGui();

	// Panel
	panel = gui.createUIPanel(CTRL_PANEL);


	// Create Softmenu
	soft = gui.createUISoft(CTRL_PANEL);
	button = gui.createUIButton(CTRL_BUTT_MENU);
	button.setText("Menu");
	soft.add(button);
	button = gui.createUIButton(CTRL_BUTT_ENTE);
	button.setText("OK");
	soft.add(button);
	button = gui.createUIButton(CTRL_BUTT_BACK);
	button.setText("Back");
	soft.add(button);

	panel.setSoft(soft);


	// Main menu
	m_MenuMain = gui.createUIMenu(CTRL_MENU_MAIN);
	//m_MenuMain.setTitle("Main");
	m_MenuMain.addItem(MENU_MAIN_EXIT, "Exit");
	m_MenuMain.addItem(MENU_MAIN_ADDC, "Add");
	m_MenuMain.addItem(MENU_MAIN_DELE, "Dele");
	m_MenuMain.addItem(MENU_MAIN_MOUP, "Up");
	m_MenuMain.addItem(MENU_MAIN_MODW, "Down");

	// Add control menu
	m_MenuAddC = gui.createUIMenu(CTRL_MENU_ADDC);
	//m_MenuAddC.setTitle("Add Ctrl");
	m_MenuAddC.addItem(MENU_ADDC_EDIT, "Edit");
	m_MenuAddC.addItem(MENU_ADDC_TEXT, "Text");
	m_MenuAddC.addItem(MENU_ADDC_TITL, "Title");
	m_MenuAddC.addItem(MENU_ADDC_LABE, "Label");
	m_MenuAddC.addItem(MENU_ADDC_NUME, "Numeric");
	m_MenuAddC.addItem(MENU_ADDC_DECI, "Decimal");
	m_MenuAddC.addItem(MENU_ADDC_PERC, "Percent");
	m_MenuAddC.addItem(MENU_ADDC_IPAD, "IP Address");
	m_MenuAddC.addItem(MENU_ADDC_REAL, "Real");
	m_MenuAddC.addItem(MENU_ADDC_BANE, "Banner");
	m_MenuAddC.addItem(MENU_ADDC_LIST, "List");
	m_MenuAddC.addItem(MENU_ADDC_COMB, "Combo");
	m_MenuAddC.addItem(MENU_ADDC_CHKB, "Check Box");
	m_MenuAddC.addItem(MENU_ADDC_RADB, "Radio Button");
	m_MenuAddC.addItem(MENU_ADDC_TIME, "Time");
	m_MenuAddC.addItem(MENU_ADDC_DATE, "Date");
	m_MenuAddC.addItem(MENU_ADDC_BACK, "Back");

	// Set Main menu to panel
	panel.setMenu(m_MenuMain);

	edit = gui.createUIEdit(panel.getId());

	String txt = "Denuncia venta ilegal de seguros y millonaria deuda a hospitales de Lima compromete a Afocat. La SBS ya cuenta con listado actual de 41 empresas";
	//edit.setText("Este es un texto");
	String texto = "";
	for (int i = 0; i < 5; i++) {
	    if (i > 0) {
		texto += "\n";
	    }
	    texto += (Integer.toString(i) + ". " + txt);
	}
	edit.setText(texto);


	panel.add(edit);

	gui.setUI(panel);
	//gui.setUI(edit);
    }

    public boolean dispatchEvent(final Event e) {
	boolean ret = false;
	switch (e.getId()) {
	case Event.UI_COMMAND:
	    ret = dispatchEventCommand(e);
	    break;
	    /*
	case Event.UI_KEY_PRESSED:
	    ret = dispatchEventKeyPressed(e);
	case Event.UI_KEY_RELEASED:
	    ret = true;
	    */
	}
	return ret;
    }

    public boolean dispatchEventKeyPressed(final Event e) {
	UIPanel panel = (UIPanel)getGui().getUI();
	UIEdit edit = (UIEdit)panel.get(CTRL_PANEL);
	edit.setText(Integer.toString(e.getWParam()));
	return true;
    }

    public boolean dispatchEventCommand(final Event e) {
	UIWidget uiw;
	boolean ret = false;
	UIPanel panel;

        //#ifdef DEBUG_MODWIDGETS
        //# System.out.println(getClass().getName() + ".dispatchEvent(Event e) ENTERED");
        //#endif        

        //#ifdef DEBUG_MODWIDGETS
        //# System.out.println("\te.getId() = " + e.getId());
        //# System.out.println("\te.getWParam() = " + e.getWParam());
        //# System.out.println("\te.getDWParam() = " + e.getDWParam());
        //# if (e.getObject() != null) {
        //#  System.out.println("\te.getObject() = " + e.getObject().getClass().getName());
        //# }
        //#endif

	switch (e.getId()) {
	case Event.UI_COMMAND:
	    // Retrive control id
	    ret = true;
	    switch (e.getWParam()) {
	    case MENU_MAIN_EXIT:
		// Exit Aplication
		break;
	    case MENU_MAIN_DELE:
		// Delete Control
		break;
	    case MENU_MAIN_MODW:
		// Move control down
		break;
	    case MENU_MAIN_MOUP:
		// Move control up
		break;
	    case MENU_MAIN_ADDC:
		// Menu add controls
		panel = (UIPanel)getGui().getUI();
		panel.setMenu(m_MenuAddC);
		panel.showMenu();
		break;
	    case MENU_ADDC_EDIT:
		// Add eontrol edit
		break;
	    case MENU_ADDC_TEXT:
		// Add control text
		break;
	    case MENU_ADDC_TITL:
		// Add control title
		break;
	    case MENU_ADDC_LABE:
		// Add control label
		break;
	    case MENU_ADDC_NUME:
		// Add control numeric
		break;
	    case MENU_ADDC_DECI:
		// Add control decimal
		break;
	    case MENU_ADDC_PERC:
		// Add control percen
		break;
	    case MENU_ADDC_IPAD:
		// Add control IP Address
		break;
	    case MENU_ADDC_REAL:
		// Add control real
		break;
	    case MENU_ADDC_BANE:
		// Add control banner
		break;
	    case MENU_ADDC_LIST:
		// Add control list
		break;
	    case MENU_ADDC_COMB:
		// Add control combo box
		break;
	    case MENU_ADDC_CHKB:
		// Add control check box
		break;
	    case MENU_ADDC_RADB:
		// Add control radio button
		break;
	    case MENU_ADDC_TIME:
		// Add control time
		break;
	    case MENU_ADDC_DATE:
		// Add control date
		break;
	    case MENU_ADDC_BACK:
		// Return main menu
		panel = (UIPanel)getGui().getUI();
		panel.setMenu(m_MenuMain);
		panel.showMenu();
		break;
	    default:
		ret = false;
	    }
	    break;
	}

        //#ifdef DEBUG_MODWIDGETS
        //# System.out.println(getClass().getName() + ".dispatchEvent(Event e) EXITING");
        //#endif        

	return ret;
    }

}